package com.dl698.axdr;

import com.dl698.asn1.axdr.AxdrType;
import com.dl698.util.ReverseByteArrayInputStream;
import com.dl698.util.ReverseByteArrayOutputStream;

/**
 * Set_Request类
 * 实现设置请求的编解码
 * 
 * @author wind
 * @version 1.1.1
 */
public class Set_Request implements AxdrType {

    private byte[] dataCode = null;
    private Choice choice = Choice._ERR_NONE_SELECTED;
    private SetRequestNormal setRequestNormal = null;
    private SetRequestNormalList setRequestNormalList = null;
    private SetThenGetRequestNormalList setThenGetRequestNormalList = null;

    public enum Choice {
        _ERR_NONE_SELECTED(-1),
        SET_REQUEST_NORMAL(1),
        SET_REQUEST_NORMAL_LIST(2),
        SET_THEN_GET_REQUEST_NORMAL_LIST(3);

        private final int value;

        Choice(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }

        public static Choice fromValue(int value) {
            for (Choice choice : Choice.values()) {
                if (choice.value == value) {
                    return choice;
                }
            }
            return _ERR_NONE_SELECTED;
        }
    }

    public Set_Request() {
    }

    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public void setSetRequestNormal(SetRequestNormal setRequestNormal) {
        this.resetChoices();
        this.choice = Choice.SET_REQUEST_NORMAL;
        this.setRequestNormal = setRequestNormal;
    }

    public void setSetRequestNormalList(SetRequestNormalList setRequestNormalList) {
        this.resetChoices();
        this.choice = Choice.SET_REQUEST_NORMAL_LIST;
        this.setRequestNormalList = setRequestNormalList;
    }

    public void setSetThenGetRequestNormalList(SetThenGetRequestNormalList setThenGetRequestNormalList) {
        this.resetChoices();
        this.choice = Choice.SET_THEN_GET_REQUEST_NORMAL_LIST;
        this.setThenGetRequestNormalList = setThenGetRequestNormalList;
    }

    @Override
    public int decode(ReverseByteArrayInputStream input) {
        int codeLength = 0;
        com.dl698.asn1.axdr.AxdrEnum choosen = new com.dl698.asn1.axdr.AxdrEnum();
        choosen.setConst();

        codeLength += choosen.decode(input);
        this.resetChoices();
        this.choice = Choice.fromValue((int) choosen.getValue());

        if (this.choice == Choice.SET_REQUEST_NORMAL) {
            this.setRequestNormal = new SetRequestNormal();
            codeLength += this.setRequestNormal.decode(input);
            return codeLength;
        }

        if (this.choice == Choice.SET_REQUEST_NORMAL_LIST) {
            this.setRequestNormalList = new SetRequestNormalList();
            codeLength += this.setRequestNormalList.decode(input);
            return codeLength;
        }

        if (this.choice == Choice.SET_THEN_GET_REQUEST_NORMAL_LIST) {
            this.setThenGetRequestNormalList = new SetThenGetRequestNormalList();
            codeLength += this.setThenGetRequestNormalList.decode(input);
            return codeLength;
        }

        throw new RuntimeException("Error decoding AxdrChoice: Identifier matched to no item.");
    }

    @Override
    public int encode(ReverseByteArrayOutputStream output) {
        if (this.dataCode != null) {
            try {
                for (int i = this.dataCode.length - 1; i >= 0; i--) {
                    output.write(this.dataCode[i]);
                }
            } catch (java.io.IOException e) {
                throw new RuntimeException("Error writing dataCode to output stream", e);
            }
            return this.dataCode.length;
        }
        if (this.choice == Choice._ERR_NONE_SELECTED) {
            throw new RuntimeException("Error encoding AxdrChoice: No item in choice was selected.");
        }

        int codeLength = 0;

        if (this.choice == Choice.SET_THEN_GET_REQUEST_NORMAL_LIST) {
            if (this.setThenGetRequestNormalList != null) {
                codeLength += this.setThenGetRequestNormalList.encode(output);
                com.dl698.asn1.axdr.AxdrEnum c = new com.dl698.asn1.axdr.AxdrEnum();
                c.setMinMaxVal(3);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new RuntimeException("setThenGetRequestNormalList is null");
            }
        } else if (this.choice == Choice.SET_REQUEST_NORMAL_LIST) {
            if (this.setRequestNormalList != null) {
                codeLength += this.setRequestNormalList.encode(output);
                com.dl698.asn1.axdr.AxdrEnum c = new com.dl698.asn1.axdr.AxdrEnum();
                c.setMinMaxVal(2);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new RuntimeException("setRequestNormalList is null");
            }
        } else if (this.choice == Choice.SET_REQUEST_NORMAL) {
            if (this.setRequestNormal != null) {
                codeLength += this.setRequestNormal.encode(output);
                com.dl698.asn1.axdr.AxdrEnum c = new com.dl698.asn1.axdr.AxdrEnum();
                c.setMinMaxVal(1);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new RuntimeException("setRequestNormal is null");
            }
        }
        throw new RuntimeException("Error encoding AxdrChoice: No item in choice was encoded.");
    }

    public void encodeAndSave(int encodingSizeGuess) {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        try {
            encode(revOStream);
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException("Error encoding Set_Request", e);
        }
        this.dataCode = revOStream.getArray();
    }

    public Choice getChoiceIndex() {
        return this.choice;
    }

    public SetRequestNormal getSetRequestNormal() {
        return this.setRequestNormal;
    }

    public SetRequestNormalList getSetRequestNormalList() {
        return this.setRequestNormalList;
    }

    public SetThenGetRequestNormalList getSetThenGetRequestNormalList() {
        return this.setThenGetRequestNormalList;
    }

    public void resetChoices() {
        this.choice = Choice._ERR_NONE_SELECTED;
        this.setRequestNormal = null;
        this.setRequestNormalList = null;
        this.setThenGetRequestNormalList = null;
    }

    @Override
    public String toString() {
        if (this.choice == Choice.SET_REQUEST_NORMAL) {
            if (this.setRequestNormal != null) {
                return "choice: {setRequestNormal: " + this.setRequestNormal + "}";
            } else {
                return "choice is setRequestNormal but setRequestNormal is null";
            }
        } else if (this.choice == Choice.SET_REQUEST_NORMAL_LIST) {
            if (this.setRequestNormalList != null) {
                return "choice: {setRequestNormalList: " + this.setRequestNormalList + "}";
            } else {
                return "choice is setRequestNormalList but setRequestNormalList is null";
            }
        } else if (this.choice == Choice.SET_THEN_GET_REQUEST_NORMAL_LIST) {
            if (this.setThenGetRequestNormalList != null) {
                return "choice: {setThenGetRequestNormalList: " + this.setThenGetRequestNormalList + "}";
            } else {
                return "choice is setThenGetRequestNormalList but setThenGetRequestNormalList is null";
            }
        } else {
            return "unknown";
        }
    }
}